package com.palfund.notification;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.RemoteInput;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.media.session.MediaSessionCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.NotificationCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {

    private NotificationCompat.Builder mBuilder;
    private NotificationManager mManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("MainActivity");
        // 通知管理器
        mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(this);
    }

    public void click(View view) {
        // Notification基础设置
        baseNotification();
        switch (view.getId()) {
            case R.id.btn_normal:
                mManager.notify(1, mBuilder.build());
                break;
            case R.id.btn_bigText:
                bigText();
                break;
            case R.id.btn_bigPicture:
                bigPicture();
                break;
            case R.id.btn_inbox:
                inbox();
                break;
            case R.id.btn_progress:
                progress();
                break;
            case R.id.btn_custom:
                custom();
                break;
            case R.id.btn_hangup:
                hangup();
                break;
            case R.id.btn_mediaStyle:
                mediaStyle();
                break;
        }
    }


    private void custom() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("Notification");
        builder.setContentText("自定义通知栏示例");
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable
        // .push));
        builder.setAutoCancel(false);
        builder.setOngoing(true);
        builder.setShowWhen(false);
        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout
                .notification_template_customer);
        remoteViews.setTextViewText(R.id.title, "Notification");
        remoteViews.setTextViewText(R.id.text, "song");
        Intent activity = new Intent(this, SecondActivity.class);
        activity.putExtra("info", "play");
        PendingIntent PIntent1 = PendingIntent.getActivity(this, 5, activity, 0);
        remoteViews.setOnClickPendingIntent(R.id.btn1, PIntent1);

        Intent service = new Intent(this, NotificationService.class);
        service.putExtra("info", "next");
        PendingIntent PIntent2 = PendingIntent.getService(this, 6, service, 0);
        remoteViews.setOnClickPendingIntent(R.id.btn2, PIntent2);

        Intent broadcast = new Intent("notification");
        broadcast.putExtra("info", "cancel");
        PendingIntent PIntent3 = PendingIntent.getBroadcast(this, 7, broadcast, 0);
        remoteViews.setOnClickPendingIntent(R.id.btn3, PIntent3);
        // 通过setContent()方法获得的Notification是定高的。如果View的高度比默认高度要大的话，就有一部分显示不出来。
        // builder.setContent(remoteViews);
        Notification notification = builder.build();
        // bigContentView是展开时的视图256dp;注意bigContentView和contentView的设置不能调转顺序
        notification.bigContentView = remoteViews;
        // contentView是收起时的视图。
        notification.contentView = remoteViews;
        mManager.notify(8, notification);
    }

    private void mediaStyle() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("MediaStyle");
        builder.setContentText("Song Title");
        builder.setSmallIcon(R.mipmap.star);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.dasheng));
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        Intent intent = new Intent(this, ThirdActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
        builder.setContentIntent(pIntent);
        //第一个参数是图标资源id 第二个是图标显示的名称，第三个图标点击要启动的PendingIntent,最多5个action
        builder.addAction(R.mipmap.ic_previous_white, "A", null);
        builder.addAction(R.mipmap.ic_stop_white, "B", null);
        builder.addAction(R.mipmap.ic_play_arrow_white_18dp, "C", null);
        builder.addAction(R.mipmap.ic_next_white, "D", null);
        NotificationCompat.MediaStyle style = new NotificationCompat.MediaStyle();
        style.setMediaSession(new MediaSessionCompat(this, "MediaSession", new ComponentName
                (MainActivity.this, Intent.ACTION_MEDIA_BUTTON), null).getSessionToken());
        //CancelButton在5.0以下的机器有效
        style.setCancelButtonIntent(pIntent);
        style.setShowCancelButton(true);
        //设置要显示在通知右方的图标 最多三个
        style.setShowActionsInCompactView(0, 1, 2);
        builder.setStyle(style);
        builder.setShowWhen(false);
        Notification notification = builder.build();
        mManager.notify(7, notification);
    }

    private void hangup() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle("横幅通知");
        builder.setContentText("请在设置通知管理中开启消息横幅提醒权限");
        builder.setDefaults(NotificationCompat.DEFAULT_ALL);
        builder.setSmallIcon(R.mipmap.star);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.dasheng));
        Intent intent = new Intent(this, ThirdActivity.class);
        intent.putExtra("info", "hangup");
        PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent, 0);
        builder.setContentIntent(pIntent);
        //
        builder.setFullScreenIntent(pIntent, true);
        builder.setAutoCancel(true);
        Notification notification = builder.build();
        mManager.notify(6, notification);
    }

    private void progress() {
        final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this);
        builderProgress.setSmallIcon(R.mipmap.star).setContentTitle("文件下载中。。。。").setContentText
                ("下载0%");
        new Thread() {
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    builderProgress.setProgress(100, i, false).setContentText("下载" + i + "%");
                    mManager.notify(1, builderProgress.build());
                    SystemClock.sleep(300);
                }
                builderProgress.setProgress(100, 100, false).setContentTitle("下载完成")
                        .setContentText("下载100%");
                mManager.notify(1, builderProgress.build());
            }
        }.start();
    }

    private void inbox() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        // 设置小图标,必须设置
        builder.setSmallIcon(R.mipmap.star);
        // 标题
        builder.setContentTitle("Happy New Year");
        // 内容
        builder.setContentText("Good Luck");
        // 设置大图标
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dasheng);
        builder.setLargeIcon(bitmap);
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        for (int i = 0; i < 5; i++) {
            inboxStyle.addLine("we will go..." + i);
        }
        builder.setStyle(inboxStyle);
        mManager.notify(4, builder.build());
    }

    private void bigPicture() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        // 设置小图标,必须设置
        builder.setSmallIcon(R.mipmap.star);
        // 标题
        builder.setContentTitle("Happy New Year");
        // 内容
        builder.setContentText("Good Luck");
        // 设置大图标
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dasheng);
        builder.setLargeIcon(bitmap);
        NotificationCompat.BigPictureStyle bigPictureStyle = new NotificationCompat
                .BigPictureStyle();
        bigPictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable
                .dasheng));
        bigPictureStyle.bigLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap
                .message));
        builder.setStyle(bigPictureStyle);
        mManager.notify(3, builder.build());
    }

    private void bigText() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        // 设置小图标,必须设置
        builder.setSmallIcon(R.mipmap.star);
        // 标题
        builder.setContentTitle("Happy New Year");
        // 内容
        builder.setContentText("Good Luck");
        // 设置大图标
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dasheng);
        builder.setLargeIcon(bitmap);
        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        bigTextStyle.bigText(getResources().getString(R.string.large_text));
        bigTextStyle.setBigContentTitle("大文本的标题");
        bigTextStyle.setSummaryText("摘要信息");
        builder.setStyle(bigTextStyle);
        mManager.notify(2, builder.build());
    }

    private void baseNotification() {
        // 设置小图标,必须设置
        mBuilder.setSmallIcon(R.mipmap.star);
        // 标题
        mBuilder.setContentTitle("Happy New Year");
        // 内容
        mBuilder.setContentText("Good Luck");
        // subText
        mBuilder.setSubText("subText");
        // 设置大图标
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dasheng);
        mBuilder.setLargeIcon(bitmap);
        // 通知栏首次出现在通知栏,短暂的提示
        mBuilder.setTicker("有新的通知请注意查看");
        // 设置收到通知的时间
        mBuilder.setWhen(System.currentTimeMillis());
        // 不显示通知收到的时间
        mBuilder.setShowWhen(true);
        // notification右下角显示的数字
        mBuilder.setNumber(8);
        // 右下角显示的信息
        mBuilder.setContentInfo("ContentInfo");
        // 用户点击后消失
        mBuilder.setAutoCancel(false);
        // 设置通知栏优先级(-2--2)
        mBuilder.setPriority(2);
        // 设置为一个正在进行的通知.他们通常是用来表示一个后台任务,用户积极参与(如播放音乐)或以某种方式正在等待
        // 通知栏左右滑动无法清除,setAutoCancel(true)时,用户点击后清除;mManager.cancelAll()可清除
        mBuilder.setOngoing(false);
        // 向通知添加声音、闪灯和振动效果的最简单、最一致的方式是使用当前的用户默认设置
        //mBuilder.setDefaults(Notification.DEFAULT_ALL);
        Intent clickIntent = new Intent(this, SecondActivity.class);
        clickIntent.putExtra("info", "click");
        // 第一个参数上下文;第二个参数请求码,请求码不同Intent就不同;第三个参数Intent;第四个参数flag:
        // FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,
        // 然后重新生成一个PendingIntent对象。
        // FLAG_ONE_SHOT:该PendingIntent只作用一次.在该PendingIntent对象通过send()方法触发过后,
        // PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
        // FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null.
        // FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingIntent,
        // 那么系统将使用该PendingIntent对象,但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据
        // 每次notifiId都是相同的数字,说明PendingIntent是一个,旧的参数被更新了.
        /**
         * Intent是立即执行，而PendingIntent可以等到事件发生后触发，PendingIntent可以cancel；
         * Intent在程序结束后即终止，而PendingIntent在程序结束后依然有效；
         * PendingIntent自带Context，而Intent需要在某个Context内运行；
         * Intent在原task中运行，PendingIntent在新的task中运行。
         */
        /**
         * 在Activity中getActivity(Context context, int requestCode, Intent intent, int flags)
         * {requestCode请求码},
         * Service中getService()，Broadcast中getBroadcast().
         */
        PendingIntent clickPendingIntent = PendingIntent.getActivity(this, 1, clickIntent,
                PendingIntent.FLAG_ONE_SHOT);
        // 设置点击操作
        mBuilder.setContentIntent(clickPendingIntent);
        Intent deleteIntent = new Intent(this, SecondActivity.class);
        deleteIntent.putExtra("info", "delete");
        PendingIntent deletePendingIntent = PendingIntent.getActivity(this, 1, deleteIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        // 设置清除操作
        mBuilder.setDeleteIntent(deletePendingIntent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 0, 1, "cancel");
        menu.add(Menu.NONE, 1, 2, "FLAG_CANCEL_CURRENT");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 0:
                //mManager.cancel(1);
                mManager.cancelAll();
                break;
            case 1:
                mBuilder.setContentIntent(PendingIntent.getActivity(this, 1, new Intent(this,
                        ThirdActivity.class), PendingIntent.FLAG_UPDATE_CURRENT)).setAutoCancel
                        (false);
                mManager.notify(1, mBuilder.build());
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void messageNotification() {
        Notification.Builder builder = new Notification.Builder(MainActivity.this);
        builder.setSmallIcon(R.mipmap.star);
        builder.setContentTitle("this is Notification Title");
        builder.setContentText("this is Notification Text");
        Intent intent = new Intent(this, SecondActivity.class);
        PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                intent, 0);
        builder.setContentIntent(mPendingIntent);
        builder.setPriority(Notification.PRIORITY_MAX);
        //Notification.MessagingStyle mMessagingStyle = new Notification.MessagingStyle
        // ("zhuyuqiang");
        //mMessagingStyle.addMessage("Message Content", 10 * 1000, "sender");
        //mMessagingStyle.setConversationTitle("10086");
        //builder.setStyle(mMessagingStyle);
        Notification notification = builder.build();
        mManager.notify(9, notification);
    }


    public void showReplyStyleNotification(View view) {
        Notification.Builder mBuilder = new Notification.Builder(MainActivity.this);
        mBuilder.setSmallIcon(R.mipmap.star);
        mBuilder.setContentTitle("this is Notification Title");
        mBuilder.setContentText("this is Notification Text");
        Intent mIntent = new Intent();
        ComponentName name = new ComponentName("com.example.notificationdemo", "com.example" + ""
                + ".notificationdemo.MainActivity");
        mIntent.setComponent(name);
        PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
                mIntent, 0);
        mBuilder.setContentIntent(mPendingIntent);
        mBuilder.setPriority(Notification.PRIORITY_MAX);
        RemoteInput remoteInput = new RemoteInput.Builder("key_text_reply").setLabel("reply " +
                "label").build();
        Notification.Action action = new Notification.Action.Builder(R.mipmap.ic_launcher,
                getString(R.string.app_name), PendingIntent.getService(MainActivity.this, 0, new
                Intent("start.notification.service"), 0)).addRemoteInput(remoteInput).build();
        mBuilder.addAction(action);
        mBuilder.setGroup("hello");
        Notification notification = mBuilder.build();
        mManager.notify(10, notification);
    }
}

